*Construct a dataset with acerecode, year, university county, and whether the university is in a normal school county

clear

*Use roster of all universities in each year to get the university's county and whether it is a normal school

use "degenrollempstaff_inst_8018.dta"

egen tagunitid = tag(unitid)
keep if tagunitid==1
keep unitid instnm cty_fips statefips normalschool_schoolfile 
rename normalschool_schoolfile prevnormsch

save "inst_cty.dta", replace

*Merge with restricted-access TFS data

clear
use "RWFinalFileStataold.dta"
gen unitid = UnitID

merge m:1 unitid using "inst_cty.dta"
rename _merge mergeinstctyname
keep if mergeinstctyname==1|mergeinstctyname==3

*Note that all the historical normal schools are in the roster...so if there is a unitid in the TFS data that does
*not merge to the roster, it is not a previous normal school
replace prevnormsch = 0 if mergeinstctyname==1 & unitid~=.

*Merge with data set denoting normal school or asylum counties
merge m:1 cty_fips using "justnormasylum.dta"
rename _merge mergeinstnorm

save TFS_unitid.dta, replace

clear 
use TFS_unitid.dta
keep acerecode year unitid cty_fips prevnormsch instnm statefips hasnormalschool hasnormalorasylum zipcode city state mergeinstctyname

gen instzip = substr(zipcode, 1, 5)

egen tagaceyear = tag(acerecode year)
keep if tagaceyear==1

save TFS_unitid_aceyear.dta, replace
